package com.vader.admin.service.impl;

import com.vader.admin.client.DistributeIdClient;
import com.vader.admin.dao.BaseUserInfoDao;
import com.vader.admin.dao.BaseUserIntegrationDao;
import com.vader.admin.entity.DO.BaseUserInfo;
import com.vader.admin.entity.DO.BaseUserIntegration;
import com.vader.admin.entity.VO.BaseUserIntegrationVO;
import com.vader.admin.service.BaseUserInfoService;
import com.vader.common.entity.CommonResponse;
import com.vader.common.exception.BadRequestException;
import com.vader.common.utils.EncryptUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Date;
import java.time.LocalDate;
import java.util.Objects;

import static com.vader.common.entity.CommonResponse.*;

/**
 * 用户信息表(BaseUserInfo)表服务实现类
 *
 * @author Vader
 * @since 2021-10-06 12:46:39
 */
@Service("baseUserInfoService")
public class BaseUserInfoServiceImpl implements BaseUserInfoService {
    @Resource
    private BaseUserInfoDao baseUserInfoDao;

    @Resource
    private DistributeIdClient distributeIdClient;

    @Resource
    private BaseUserIntegrationDao baseUserIntegrationDao;

    private static final int PASSWORD_LENGTH = 8;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public BaseUserIntegrationVO queryById(Long id) {
        BaseUserInfo baseUserInfo = baseUserInfoDao.queryById(id);
        BaseUserIntegration baseUserIntegration = baseUserIntegrationDao.queryById(id);
        return BaseUserIntegrationVO.from(baseUserInfo, baseUserIntegration);
    }

    /**
     * 分页查询
     *
     * @param baseUserInfo 筛选条件
     * @param pageRequest  分页对象
     * @return 查询结果
     */
    @Override
    public Page<BaseUserInfo> queryByPage(BaseUserInfo baseUserInfo, PageRequest pageRequest) {
        long total = this.baseUserInfoDao.count(baseUserInfo);
        return new PageImpl<>(this.baseUserInfoDao.queryAllByLimit(baseUserInfo, pageRequest), pageRequest, total);
    }

    /**
     * 新增用户
     *
     * @param baseUserInfo 实例对象
     * @return 实例对象
     */
    @Override
    public CommonResponse<BaseUserInfo> insert(BaseUserInfo baseUserInfo) {
        //创建用户
        CommonResponse<Long> uid = distributeIdClient.getId();
        baseUserInfo.setId(uid.getData());
        baseUserInfo.setCreateTime(Date.valueOf(LocalDate.now()));
        baseUserInfo.setUpdateTime(Date.valueOf(LocalDate.now()));

        if (baseUserInfo.getUserType() == 0) {
            String password = baseUserInfo.getPassword();
            if (password.length() < PASSWORD_LENGTH) {
                throw new BadRequestException("密码长度必须大于等于八位");
            }
            try {
                baseUserInfo.setPassword(EncryptUtils.desEncrypt(password));
            } catch (Exception e) {
                throw new BadRequestException(e.getMessage());
            }
        }
        baseUserInfoDao.insert(baseUserInfo);

        //创建积分系统
        BaseUserIntegration baseUserIntegration = BaseUserIntegration.init();
        baseUserIntegration.setUid(uid.getData());
        baseUserIntegrationDao.insert(baseUserIntegration);

        return success(baseUserInfo);
    }

    /**
     * 用户信息修改
     *
     * @param baseUserInfo 实例对象
     * @return 实例对象
     */
    @Override
    public BaseUserInfo update(BaseUserInfo baseUserInfo) {
        this.baseUserInfoDao.update(baseUserInfo);
        return baseUserInfoDao.queryById(baseUserInfo.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        BaseUserInfo baseUserInfo = baseUserInfoDao.queryById(id);
        if (Objects.isNull(baseUserInfo)) {
            throw new BadRequestException("用户不存在");
        }
        baseUserInfo.setDeleted(1);
        return baseUserInfoDao.update(baseUserInfo) > 0;
    }
}
